macro(copy_release_dir INPUT INPUTDIR)
  if (EXISTS ${INPUTDIR}/${INPUT})
    if (MINGW OR NMAKE)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${INPUT} COPYONLY)
	else ()
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/release/${INPUT} COPYONLY)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/relwithdebinfo/${INPUT} COPYONLY)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/minsizerel/${INPUT} COPYONLY)
	endif ()
  endif ()
endmacro ()

macro(copy_release INPUT)
	if ("${INPUT}" STREQUAL "")
		message("Cannot copy an empty string!")
	else()
		if(EXISTS ${INPUT})
			message(STATUS "copying ${INPUT} to release..")
			get_filename_component(INPUTDIR ${INPUT} PATH)
			get_filename_component(INPUTNAME ${INPUT} NAME)
			copy_release_dir(${INPUTNAME} ${INPUTDIR})
		else()
			message(STATUS "${INPUT} does not exist!")		
		endif()			
	endif()
endmacro()

macro(configure_release_dir INPUT INPUTDIR)
  if (EXISTS ${INPUTDIR}/${INPUT})
    if (MINGW OR NMAKE OR UNIX)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${INPUT} @ONLY)
	else ()
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/release/${INPUT} @ONLY)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/relwithdebinfo/${INPUT} @ONLY)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/minsizerel/${INPUT} @ONLY)
	endif ()
  endif ()
endmacro ()

macro(copy_release_debug INPUT INPUTDIR)
  if (EXISTS ${INPUTDIR}/${INPUT})
    if (MINGW OR NMAKE OR UNIX)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${INPUT} COPYONLY)
	else ()
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/release/${INPUT} COPYONLY)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/relwithdebinfo/${INPUT} COPYONLY)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/minsizerel/${INPUT} COPYONLY)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/debug/${INPUT} COPYONLY)
	endif ()
 endif ()
endmacro ()

macro(copy_debug_dir INPUT INPUTDIR)
  if (EXISTS ${INPUTDIR}/${INPUT})
    if (MINGW OR NMAKE)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${INPUT} COPYONLY)
	else ()
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/debug/${INPUT} COPYONLY)
	endif ()
  endif ()
endmacro()

macro(configure_debug_dir INPUT INPUTDIR)
  if (EXISTS ${INPUTDIR}/${INPUT})
    if (MINGW OR NMAKE OR UNIX)
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${INPUT} @ONLY)
	else ()
      configure_file(${INPUTDIR}/${INPUT} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/debug/${INPUT} @ONLY)
	endif ()
  endif ()
endmacro()

macro(copy_debug INPUT)
	if ("${INPUT}" STREQUAL "")
		message("Cannot copy an empty string!")
	else()
		if(EXISTS ${INPUT})
			message(STATUS "copying ${INPUT} to debug..")
			get_filename_component(INPUTDIR ${INPUT} PATH)
			get_filename_component(INPUTNAME ${INPUT} NAME)
			copy_debug_dir(${INPUTNAME} ${INPUTDIR})
		else()
			message(STATUS "${INPUT} does not exist!")		
		endif()
	endif()
endmacro()

macro (prepare_demo_dirs)
	find_package(OIS REQUIRED)

if(NOT OIS_FOUND)
	message(SEND_ERROR "Failed to find OIS.")
endif()
  
# if win32, copy the Ogre DLLs over
if(WIN32)
	copy_release("${OGRE_BINARY_REL}")	
	copy_debug("${OGRE_BINARY_DBG}")
	copy_release("${OGRE_RTShaderSystem_BINARY_REL}")
	copy_debug("${OGRE_RTShaderSystem_BINARY_DBG}")
	
	copy_release("${OGRE_RenderSystem_Direct3D9_REL}")
	copy_debug("${OGRE_RenderSystem_Direct3D9_DBG}")
		
	copy_release("${OGRE_RenderSystem_Direct3D11_REL}")
	copy_debug("${OGRE_RenderSystem_Direct3D11_DBG}")
		
	copy_release("${OGRE_RenderSystem_GL_REL}")	
	copy_debug("${OGRE_RenderSystem_GL_DBG}")
	
	copy_release("${OGRE_RenderSystem_GL3Plus_REL}")	
	copy_debug("${OGRE_RenderSystem_GL3Plus_DBG}")
	
	copy_release("${OIS_BINARY_REL}")	
	copy_debug("${OIS_BINARY_DBG}")		

    if ((${OGRE_VERSION} VERSION_EQUAL "1.9.0") OR (${OGRE_VERSION} VERSION_GREATER "1.9.0"))
		copy_release("${OGRE_Overlay_BINARY_REL}")	
		copy_debug("${OGRE_Overlay_BINARY_DBG}")
    endif()
	
	#MingW doesn't want to run samples if boost dlls are not present (but MSVC is ok with that..)
	FILE(GLOB BOOST_DEB_DLLS "${OGRE_PLUGIN_DIR_DBG}/*boost*.dll")
	foreach(DLL ${BOOST_DEB_DLLS})
		GET_FILENAME_COMPONENT(SHORT_DLL ${DLL} NAME)
		copy_debug(${SHORT_DLL})
	endforeach(DLL)
	FILE(GLOB BOOST_REL_DLLS "${OGRE_PLUGIN_DIR_REL}/*boost*.dll")
	foreach(DLL ${BOOST_REL_DLLS})
		GET_FILENAME_COMPONENT(SHORT_DLL ${DLL} NAME)
		copy_release(${SHORT_DLL})
	endforeach(DLL)
		
	set(OgreProcedural_PLUGIN_DIR_REL ".")
	set(OgreProcedural_PLUGIN_DIR_DBG ".")

	set(OGRE_PLUGIN_DIR_REL ".")
	set(OGRE_PLUGIN_DIR_DBG ".")
	
	
endif(WIN32)

# if Linux, copy the Ogre configuration files over
if(UNIX AND NOT APPLE)

	set(OgreProcedural_PLUGIN_DIR_REL ".")
	set(OgreProcedural_PLUGIN_DIR_DBG ".")
	
	#comes from findOgre.cmake (don't know why it doesn't work there...)
	get_filename_component(OGRE_PLUGIN_DIR_REL ${OGRE_RenderSystem_GL_LIBRARY_REL} PATH)
	get_filename_component(OGRE_PLUGIN_DIR_DBG ${OGRE_RenderSystem_GL_LIBRARY_DBG} PATH)		

endif(UNIX AND NOT APPLE)

if(DEFINED OgreProcedural_NO_D3D OR (NOT EXISTS ${OGRE_RenderSystem_Direct3D9_DBG}))
	set(OgreProcedural_COMMENT_RENDERSYSTEM_D3D9_D "#")
endif()
if(DEFINED OgreProcedural_NO_D3D OR NOT EXISTS ${OGRE_RenderSystem_Direct3D9_REL})
	set(OgreProcedural_COMMENT_RENDERSYSTEM_D3D9 "#")
endif()
if(DEFINED OgreProcedural_NO_D3D OR NOT EXISTS ${OGRE_RenderSystem_Direct3D11_DBG})
	set(OgreProcedural_COMMENT_RENDERSYSTEM_D3D11_D "#")
endif()
if(DEFINED OgreProcedural_NO_D3D OR NOT EXISTS ${OGRE_RenderSystem_Direct3D11_REL})
	set(OgreProcedural_COMMENT_RENDERSYSTEM_D3D11 "#")
endif()
if(NOT EXISTS ${OGRE_RenderSystem_GL_DBG})
	set(OgreProcedural_COMMENT_RENDERSYSTEM_GL_D "#")
endif()
if(NOT EXISTS ${OGRE_RenderSystem_GL_REL})
	set(OgreProcedural_COMMENT_RENDERSYSTEM_GL "#")
endif()
if(NOT EXISTS ${OGRE_RenderSystem_GL3Plus_DBG})
	set(OgreProcedural_COMMENT_RENDERSYSTEM_GL3Plus_D "#")
endif()
if(NOT EXISTS ${OGRE_RenderSystem_GL3Plus_REL})
	set(OgreProcedural_COMMENT_RENDERSYSTEM_GL3Plus "#")
endif()

set(OgreProcedural_MEDIA_DIR "${OgreProcedural_SOURCE_DIR}/samples/media")

configure_release_dir(resources.cfg ${OgreProcedural_TEMPLATES_DIR})
configure_debug_dir(resources.cfg ${OgreProcedural_TEMPLATES_DIR})

configure_release_dir(plugins.cfg ${OgreProcedural_TEMPLATES_DIR})
configure_debug_dir(plugins_d.cfg ${OgreProcedural_TEMPLATES_DIR})
endmacro()